package com.liao.note.service.impl;

import com.liao.note.dao.NoteDao;
import com.liao.note.domain.Note;
import com.liao.note.service.NoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;

@Service
public class NoteServiceImpl implements NoteService {
    @Autowired
    private NoteDao noteDao;

    @Override
    public ArrayList<Note> queryByUserId(Integer userId) {
        ArrayList<Note> notes = noteDao.queryByUserId(userId);
        return notes;
    }

    @Override
    public ArrayList<Note> queryByCategoryId(Integer categoryId) {
        return noteDao.queryByCategoryId(categoryId);
    }

    @Override
    public ArrayList<Note> queryDeleteByUserId(Integer userId) {
        return noteDao.queryDeleteByUserId(userId);
    }

    @Override
    public ArrayList<Note> queryAllShare(Integer userId) {
        return noteDao.queryAllShare(userId);
    }

    @Override
    public int addNote(Note note) {
        note.setCreateDate(new Date());
        note.setSize(note.getContent().length());
        return noteDao.addNote(note);
    }

    @Override
    public int updateNote(Note note) {
        if (note.getContent() != null)
            note.setSize(note.getContent().length());
        return noteDao.updateNote(note);
    }

    @Override
    public int deleteById(Integer id) {
        return noteDao.deleteById(id);
    }

    @Override
    public boolean deleteAllNote(int userId) {
        int i = noteDao.deleteAllNote(userId);
        return i > 0;
    }

}
